//------------------------------------------------------------------------------
// <copyright file="ViewModelStyleSelector.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace DotBeer.UI.WPFClient
{
    public class ViewModelStyleSelector : FrameworkElement
    {
        #region properties

        public static readonly DependencyProperty TargetElementProperty =
            DependencyProperty.Register("TargetElement",
                                        typeof(FrameworkElement),
                                        typeof(ViewModelStyleSelector));

        public FrameworkElement TargetElement
        {
            get { return (FrameworkElement)this.GetValue(TargetElementProperty); }
            set { this.SetValue(TargetElementProperty, value); }
        }

        public static readonly DependencyProperty DefaultStyleProperty =
            DependencyProperty.Register("DefaultStyle",
                                        typeof(Style),
                                        typeof(ViewModelStyleSelector));

        public Style DefaultStyle
        {
            get { return (Style)this.GetValue(DefaultStyleProperty); }
            set { this.SetValue(DefaultStyleProperty, value); }
        }

        public static readonly DependencyProperty HighlightedStyleProperty =
            DependencyProperty.Register("HighlightedStyle",
                                        typeof(Style),
                                        typeof(ViewModelStyleSelector));

        public Style HighlightedStyle
        {
            get { return (Style)this.GetValue(HighlightedStyleProperty); }
            set { this.SetValue(HighlightedStyleProperty, value); }
        }

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source",
                                        typeof(object),
                                        typeof(ViewModelStyleSelector),
                                        new PropertyMetadata(null, Source_Changed));

        public object Source
        {
            get { return this.GetValue(SourceProperty); }
            set { this.SetValue(SourceProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value",
                                        typeof(string),
                                        typeof(ViewModelStyleSelector));

        public string Value
        {
            get { return (string)this.GetValue(ValueProperty); }
            set { this.SetValue(ValueProperty, value); }
        }

        #endregion

        #region constructors

        public ViewModelStyleSelector()
        {
            this.Visibility = Visibility.Collapsed;
            this.Width = 0;
            this.Height = 0;
        }

        #endregion

        #region private methods

        private static void Source_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ViewModelStyleSelector styleSelector = d as ViewModelStyleSelector;

            if (styleSelector.TargetElement != null)
            {
                if (styleSelector.Value == styleSelector.Source.ToString())
                    styleSelector.TargetElement.Style = styleSelector.HighlightedStyle;
                else
                    styleSelector.TargetElement.Style = styleSelector.DefaultStyle;
            }
        }

        #endregion
    }
}
